import Vue from 'vue'
import Router from 'vue-router'
import Routes from './routes'

Vue.use(Router)

export const getRouterData = () => {
  const routes = Object.values(Routes).map(item => {
    return {
      path: item.path,
      name: item.name,
      component: () => import(`../${item.component}`),
      meta: item.meta
    };
  });
  console.log(routes)
  const router = new Router({
    mode: "hash",
    routes: [
      {
        path: '/login',
        name: 'login',
        component: () => import(`../components/login.vue`),
        meta: {
          title: "登录",
          keepAlive:true
        }
      },
      {
        path: '/register',
        name: 'register',
        component: () => import(`../components/register.vue`),
        meta: {
          title: "注册",
          keepAlive:true

        }
      },
      // {
      //   path: '/resetpassword',
      //   name: 'resetpassword',
      //   component: () => import(`../components/resetpassword.vue`),
      //   meta: {
      //     title: "重置密码",
      //     keepAlive:true
      //   }
      // },
      {
        path: '/findpassword',
        name: 'findpassword',
        component: () => import(`../components/findpassword.vue`),
        meta: {
          title: "重置密码",
          keepAlive:true
        }
      },
      {
        path: '/',
        name: 'user',
        component: () => import(`../pages/user/index.vue`),
        meta: {
          title: "用户",
          keepAlive:true
        }
      },
      {
        path: '/shop',
        name: 'shop',
        component: () => import(`../pages/shop/shopindex.vue`),
        meta: {
          title: "商家",
          keepAlive:true
        }
      },
      {
        path: '/user/storedetail',
        name: 'storedetail',
        component: () => import(`../pages/user/storedetail.vue`),
        meta: {
          title: "店铺详情",
        }
      },
      {
        path: '/history',
        name: 'history',
        component: () => import(`../pages/user/history.vue`),
        meta: {
        title: "搜索"
        }
      },
      {
        path: '/ride',
        name: 'ride',
        component: () => import(`../pages/ride/rideindex.vue`),
        meta: {
          title: "骑手",
          keepAlive:true
        }
      },
      {
        path: '/other',
        name: 'other',
        component: () => import(`../pages/user/otherIndex.vue`),
        children: routes,
        meta: {
          title: "外卖",
          keepAlive:true
        }
      }
    ]
  })
  afterRouter(router)
  return router
}
function afterRouter(router) {
  router.afterEach(transition => {
    document.title = transition.meta.title;
  });
}
